﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Runtime.InteropServices;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Security;
using System.Data;
using System.Reflection;

namespace Linxiao.SP.WebParts.SPPDFWebPart.SPPDFWebPart
{
    [Guid("9E301D93-56F2-43C4-A8BB-F20B46EA0E31")]
    [ToolboxItemAttribute(false)]
    public class SPPDFWebPart : Microsoft.SharePoint.WebPartPages.WebPart
    {
        /// <summary>
        /// Url of the target PDF file 
        /// </summary>
        private string pdfUrl = "http://partners.adobe.com/public/developer/en/xml/AdobeXMLFormsSamples.pdf";

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [WebDisplayName("PDF Url")]
        [SPWebCategoryName("PDF Settings")]
        [WebDescription("Url of the target PDF file")]
        public string PDFUrl
        {
            get { return pdfUrl; }
            set { pdfUrl = value; }
        }

        /// <summary>
        /// Width of the PDF 
        /// </summary>
        private string pdfWidth = "700";

        [Personalizable(PersonalizationScope.Shared)]
        [WebBrowsable(true)]
        [SPWebCategoryName("PDF Settings")]
        [WebDisplayName("PDF Width")]
        [WebDescription("Width of the PDF")]
        public string PDFWidth
        {
            get { return pdfWidth; }
            set { pdfWidth = value; }
        }

        /// <summary>
        /// Height of the PDF
        /// </summary>
        private string pdfHeight = "800";

        [Personalizable(PersonalizationScope.User)]
        [WebBrowsable(true)]
        [SPWebCategoryName("PDF Settings")]
        [WebDisplayName("PDF Height")]
        [WebDescription("Height of the PDF")]
        public string PDFHeight
        {
            get { return pdfHeight; }
            set { pdfHeight = value; }
        }

        /// <summary>
        /// Whether or not a connection point has been enabled
        /// </summary>
        public bool ConnectionPointEnabled
        {
            get
            {
                object o = ViewState["ConnectionPointEnabled"];
                return (o != null) ? (bool)o : true;
            }
            set
            {
                ViewState["ConnectionPointEnabled"] = value;
            }
        }

        //Connection provider which implements IWebPartField
        private IWebPartField connectionProvider;

        //The value passed in from provider webpart
        private object passedInValue;
        private void GetFieldValue(object passedInValue)
        {
            this.passedInValue = passedInValue;
        }

        /// <summary>
        /// Get the value passed in from provider web part on the current page preRender event
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            if (connectionProvider != null)
            {
                connectionProvider.GetFieldValue(new FieldCallback(GetFieldValue));
            }
            base.OnPreRender(e);
        }

        /// <summary>
        /// Render PDF embeded object
        /// </summary>
        /// <param name="writer"></param>
        protected override void RenderContents(HtmlTextWriter writer)
        {
            //Check if the web part has been connected
            if (connectionProvider != null)
            {
                //Check if there is data passed in
                if (connectionProvider.Schema != null && passedInValue != null)
                {
                    //Set the PDFUrl property as the pdf url passed in
                    pdfUrl = SPContext.Current.Site.Url + passedInValue;
                }
            }

            //Render PDF file
            string pdfScript = string.Format(
                "<object data='{0}' type='application/pdf' width='{1}' height='{2}'></object>", 
                    pdfUrl, pdfWidth, pdfHeight);
            writer.Write(pdfScript);
        }

        [ConnectionConsumer("FieldConsumer", "Connpoint1",
        typeof(FieldConsumerConnectionPoint), AllowsMultipleConnections = true)]
        public void SetConnectionInterface(IWebPartField provider)
        {
            connectionProvider = provider;
        }

        public class FieldConsumerConnectionPoint : ConsumerConnectionPoint
        {
            public FieldConsumerConnectionPoint(MethodInfo callbackMethod,
                Type interfaceType, Type controlType, string name, string id,
                bool allowsMultipleConnections)
                : base(
                callbackMethod, interfaceType, controlType,
                name, id, allowsMultipleConnections)
            {
            }

            public override bool GetEnabled(Control control)
            {
                return ((SPPDFWebPart)control).ConnectionPointEnabled;
            }

        }
        

    }
}
